#include"process.h"
#include<string.h>
#include<unistd.h>


#define SIZE 101//包含\0
#define MAX_RATE 100
#define SYTLE '#'
#define STIME 1000*40
const char* str="|/-\\";

void process_v1()
{
    //verson1
    int rate=0;
    char bar[SIZE];
    int num=strlen(str);
    memset(bar,'\0',sizeof(bar));
    while(rate<=MAX_RATE)
    {
        printf("[%-100s][%d%%][%c]\r",bar,rate,str[rate%num]);
        fflush(stdout);//缓冲区刷新一下
        usleep(STIME);
        bar[rate++]=SYTLE;
    }
    printf("\n");
}

void process_v2(int rate)
{
    //verson2
    static char bar[SIZE]={0};
    int num=strlen(str);
    //memset(bar,'\0',sizeof(bar));
    if(rate<=MAX_RATE&&rate>=0)
    {
        printf("[%-100s][%d%%][%c]\r",bar,rate,str[rate%num]);
        fflush(stdout);//缓冲区刷新一下
        bar[rate]=SYTLE;
    }
    if(rate==MAX_RATE) memset(bar,'\0',sizeof(bar));
}

void process_v3(double rate)
{
    //verson3
    static char bar[SIZE]={0};
    int num=strlen(str);
    static int cnt=0;
    if(rate<=MAX_RATE&&rate>=0)
    {
        cnt++;
        cnt=(cnt>=num?0:cnt);
        printf("[\033[32;42m%-100s\033[0m][%.1f%%][%c]\r",bar,rate,str[cnt]);
        fflush(stdout);//缓冲区刷新一下
        if(rate<MAX_RATE)
        {
            bar[(int)rate++]=STYLE_BODY;
            bar[(int)rate]=STYLE_HEADER;
        }
        else
        {
            bar[(int)rate]=STYLE_BODY;
        }
    }
   
    //if(rate==MAX_RATE) memset(bar,'\0',sizeof(bar));
}